home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / chatter / chatdoc.cpp next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  6.4 KB  |  323 lines

  1. // chatdoc.cpp : implementation of the CChatDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "chatter.h"
  15.  
  16. #include "chatsock.h"
  17. #include "chatdoc.h"
  18. #include "chatvw.h"
  19. #include "setupdlg.h"
  20. #include "msg.h"
  21.  
  22. #ifdef _WIN32
  23. #ifndef _UNICODE
  24. #include <strstrea.h>
  25. #endif
  26. #endif
  27.  
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char BASED_CODE THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CChatDoc
  35.  
  36. IMPLEMENT_DYNCREATE(CChatDoc, CDocument)
  37.  
  38. BEGIN_MESSAGE_MAP(CChatDoc, CDocument)
  39.     //{{AFX_MSG_MAP(CChatDoc)
  40.     //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CChatDoc construction/destruction
  45.  
  46. CChatDoc::CChatDoc()
  47. {
  48.     m_bAutoChat = FALSE;
  49.     m_pSocket = NULL;
  50.     m_pFile = NULL;
  51.     m_pArchiveIn = NULL;
  52.     m_pArchiveOut = NULL;
  53. }
  54.  
  55. CChatDoc::~CChatDoc()
  56. {
  57. }
  58.  
  59. BOOL CChatDoc::OnNewDocument()
  60. {
  61.     if (!CDocument::OnNewDocument())
  62.         return FALSE;
  63.  
  64. #ifdef _WIN32
  65. #ifndef _UNICODE
  66.     if (AfxGetApp()->m_lpCmdLine[0] != '\0')
  67.     {
  68.         TCHAR strHandle[128];
  69.         TCHAR strServer[128];
  70.         int nChannel;
  71.  
  72.         istrstream(AfxGetApp()->m_lpCmdLine) >> strHandle >> strServer >> nChannel;
  73.         return ConnectSocket(strHandle, strServer, nChannel);
  74.     }
  75.     else
  76. #endif
  77. #endif
  78.     {
  79.         CSetupDlg Dialog;
  80.  
  81.         Dialog.m_strHandle=m_strHandle;
  82.         Dialog.m_strServer=_T("");
  83.         Dialog.m_nChannel=0;
  84.  
  85.         while(TRUE)
  86.         {
  87.             if (IDOK != Dialog.DoModal())
  88.                 return FALSE;
  89.  
  90.             if (ConnectSocket(Dialog.m_strHandle, Dialog.m_strServer, Dialog.m_nChannel))
  91.                 return TRUE;
  92.  
  93.             CString str;
  94.             str.LoadString(IDS_CHANGEADDRESS);
  95.             if (AfxMessageBox((LPCTSTR)str,MB_YESNO) == IDNO)
  96.                 return FALSE;
  97.         }
  98.     }
  99. }
  100.  
  101. void CChatDoc::DeleteContents() 
  102. {
  103.     m_bAutoChat = FALSE;
  104.  
  105.     if ((m_pSocket != NULL) && (m_pFile != NULL) &&    (m_pArchiveOut != NULL))
  106.     {
  107.         CMsg msg;
  108.         CString strTemp;
  109.  
  110.         if (strTemp.LoadString(IDS_DISCONNECT))
  111.         {
  112.             msg.m_bClose = TRUE;
  113.             msg.m_strText = m_strHandle + strTemp;
  114.             msg.Serialize(*m_pArchiveOut);
  115.             m_pArchiveOut->Flush();
  116.         }
  117.     }
  118.                                                                     
  119.     delete m_pArchiveOut;
  120.     m_pArchiveOut = NULL;
  121.     delete m_pArchiveIn;
  122.     m_pArchiveIn = NULL;
  123.     delete m_pFile;
  124.     m_pFile = NULL;
  125.  
  126.     if (m_pSocket != NULL)
  127.     {
  128.         BYTE Buffer[50];
  129.         m_pSocket->ShutDown();
  130.  
  131.         while(m_pSocket->Receive(Buffer,50) > 0);
  132.     }
  133.  
  134.     delete m_pSocket;
  135.     m_pSocket = NULL;
  136.  
  137.     for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
  138.     {
  139.         CView* pView = GetNextView(pos);
  140.         
  141.         if (pView->IsKindOf(RUNTIME_CLASS(CChatView)))
  142.         {
  143.             CChatView* pChatView = (CChatView*)pView;
  144.             pChatView->GetEditCtrl().SetWindowText(_T(""));
  145.         }    
  146.     }
  147.     CDocument::DeleteContents();
  148. }
  149.  
  150. /////////////////////////////////////////////////////////////////////////////
  151. // CChatDoc Operations
  152.  
  153. BOOL CChatDoc::ConnectSocket(LPCTSTR lpszHandle, LPCTSTR lpszAddress, UINT nPort)
  154. {
  155.     m_strHandle = lpszHandle;
  156.  
  157.     m_pSocket = new CChatSocket(this, CCeSocket::FOR_DATA);
  158.  
  159.     if (!m_pSocket->Create())
  160.     {
  161.         delete m_pSocket;
  162.         m_pSocket = NULL;
  163.         CString str;
  164.         str.LoadString(IDS_CREATEFAILED);
  165.         AfxMessageBox((LPCTSTR)str);
  166.         return FALSE;
  167.     }
  168.  
  169.     while (!m_pSocket->Connect(lpszAddress, nPort + 700))
  170.     {
  171.         CString str;
  172.         str.LoadString(IDS_RETRYCONNECT);
  173.         if (AfxMessageBox((LPCTSTR)str,MB_YESNO) == IDNO)
  174.         {
  175.             delete m_pSocket;
  176.             m_pSocket = NULL;
  177.             return FALSE;
  178.         }
  179.     }
  180.  
  181.     m_pFile = new CSocketFile(m_pSocket);
  182.     m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
  183.     m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
  184.  
  185.     CString strTemp;
  186.     if (strTemp.LoadString(IDS_CONNECT))
  187.         SendMsg(strTemp
  188.         );
  189.  
  190.     return TRUE;
  191. }
  192.  
  193. void CChatDoc::ProcessPendingRead() 
  194. {
  195.     do
  196.     {
  197.         ReceiveMsg();
  198.         if (m_pSocket == NULL)
  199.             return;
  200.     }
  201.     while(!m_pArchiveIn->IsBufferEmpty());
  202. }
  203.  
  204. void CChatDoc::SendMsg(CString& strText)
  205. {
  206.     if (m_pArchiveOut != NULL)
  207.     {
  208.         CMsg msg;
  209.  
  210.         msg.m_strText = m_strHandle + _T(": ") + strText;
  211.  
  212.         TRY
  213.         {
  214.             msg.Serialize(*m_pArchiveOut);
  215.             m_pArchiveOut->Flush();
  216.         }
  217.         CATCH(CFileException, e)
  218.         {
  219.             m_bAutoChat = FALSE;
  220.             m_pArchiveOut->Abort();
  221.             delete m_pArchiveOut;
  222.             m_pArchiveOut = NULL;
  223.         
  224.             CString strTemp;
  225.             if (strTemp.LoadString(IDS_SERVERRESET))
  226.                 DisplayMsg(strTemp);
  227.         }
  228.         END_CATCH
  229.     }
  230. }
  231.  
  232. void CChatDoc::ReceiveMsg()
  233. {
  234.     CMsg msg;
  235.     
  236.     TRY
  237.     {
  238.         msg.Serialize(*m_pArchiveIn);
  239.  
  240.         while(!msg.m_msgList.IsEmpty())
  241.         {
  242.             CString temp = msg.m_msgList.RemoveHead();
  243.             DisplayMsg(temp);
  244.         }
  245.  
  246.     }
  247.     CATCH(CFileException, e)
  248.     {
  249.         m_bAutoChat = FALSE;
  250.         msg.m_bClose = TRUE;
  251.         m_pArchiveOut->Abort();
  252.  
  253.         CString strTemp;
  254.         if (strTemp.LoadString(IDS_SERVERRESET))
  255.             DisplayMsg(strTemp);
  256.         if (strTemp.LoadString(IDS_CONNECTIONCLOSED))
  257.             DisplayMsg(strTemp);
  258.     }
  259.     END_CATCH
  260.  
  261.     if (msg.m_bClose)
  262.     {
  263.         delete m_pArchiveIn;
  264.         m_pArchiveIn = NULL;
  265.         delete m_pArchiveOut;
  266.         m_pArchiveOut = NULL;
  267.         delete m_pFile;
  268.         m_pFile = NULL;
  269.         delete m_pSocket;
  270.         m_pSocket = NULL;
  271.     }
  272. }
  273.  
  274. void CChatDoc::DisplayMsg(LPCTSTR lpszText)
  275. {
  276.  
  277.     for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
  278.     {
  279.         CView* pView = GetNextView(pos);
  280.         CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
  281.         
  282.         if (pChatView != NULL)
  283.             pChatView->Message(lpszText);
  284.     }
  285. }
  286.  
  287. /////////////////////////////////////////////////////////////////////////////
  288. // CChatDoc serialization
  289.  
  290. void CChatDoc::Serialize(CArchive& ar)
  291. {
  292.     if (ar.IsStoring())
  293.     {
  294.         for(POSITION pos=GetFirstViewPosition();pos!=NULL;)
  295.         {
  296.             CView* pView = GetNextView(pos);
  297.             CChatView* pChatView = DYNAMIC_DOWNCAST(CChatView, pView);
  298.         
  299.             if (pChatView != NULL)
  300.                 pChatView->SerializeRaw(ar);
  301.         }
  302.     }
  303. }
  304.  
  305. /////////////////////////////////////////////////////////////////////////////
  306. // CChatDoc diagnostics
  307.  
  308. #ifdef _DEBUG
  309. void CChatDoc::AssertValid() const
  310. {
  311.     CDocument::AssertValid();
  312. }
  313.  
  314. void CChatDoc::Dump(CDumpContext& dc) const
  315. {
  316.     CDocument::Dump(dc);
  317. }
  318. #endif //_DEBUG
  319.  
  320. /////////////////////////////////////////////////////////////////////////////
  321. // CChatDoc commands
  322.  
  323.